<template>
  <el-form class="login-form"
           status-icon
           :rules="loginRules"
           ref="loginForm"
           :model="loginForm"
           label-width="0">
    <el-form-item prop="pincode">
      <el-input size="small"
                @keyup.enter.native="handleLogin"
                :type="pincodeType"
                v-model="loginForm.pincode"
                auto-complete="off"
                :placeholder="$t('请输入pin码')"
                :disabled = "disabled"
                ref="pincode"
                >
        <i class="el-icon-view el-input__icon"
           slot="suffix"
           @click="showpincode"></i>
        <i slot="prefix"
           class="el-icon-lock"></i>
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary"
                 @click.native.prevent="handleLogin"
                 :loading="isLoading"
                 class="login-submit">{{submitName}}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from "vuex"
export default {
  name: "userlogin",
  data() {
    return {
      pincode: "",//key验证pin码
      debug:true,//是否调试-暂不用
      caserverip:"",//CA服务IP
      strrandom:"463971389",//服务器下送的随机串
      userconfirm:"",//用户批准
      loginrtn:null,//登陆验证信息
      loginForm: {//CA登录信息
        caUserSign:"",//CA登录名
        caCer: "",//usbkey的证书
        caRandom:"",//服务器下送的随机串
      },
      loginRules: {
        pincode: [
          { required: true, message: this.$t("请输入pin码"), trigger: "blur" }
        ]
      },
      pincodeType: "password",
      isLoading:false,
      disabled : false,
      submitName: this.$t('登录')
    }
  },
  created() {
  },
  mounted() {
  },
  computed: {
    ...mapGetters(["tagWel","appConfig"])
  },
  props: [],
  methods: {
    showpincode() {
      this.pincodeType == ""
        ? (this.pincodeType = "password")
        : (this.pincodeType = "")
    },
    loading(flag){
      if(flag){
          this.submitName = this.$t('登录中...')
          this.isLoading = true
          this.disabled = true
      }
      else{
            this.submitName = this.$t('登录')
            this.isLoading = false
            this.disabled = false
      }
    },
    //CA登录----ADD BY SSY 20230426
    //获取usbkey的证书函数
    getCer(){
      var rtn=window.usb_ocx.readCert("3","1");
      if(rtn==""){
        this.$message({
          type: "danger",
          message: this.$t("得到的UsbKey的证书错误!"),
        });
      }else{
        this.loginForm.caCer=rtn;
      }
    },
    //根据服务器下送的随机串+登录名做hash，同时生成签名
    getSignUser(m_User,m_random){
      var rtn1=window.usb_ocx.concat(m_User,"","");
      var rtn2=window.usb_ocx.concat(m_random,rtn1,"");
      var rtn3=window.usb_ocx.dataSignCertEx(rtn2,"1","0");
      this.loginForm.caUserSign=rtn3;
      this.loginForm.caRandom=m_random;
    },
    //提交给服务器进行判断验证处理
    handleLogin() {
      try{
        this.loginrtn=window.usb_ocx.VgetnameFirst(this.caserverip,"1");
      }catch(error){
        this.$message({
          type: "danger",
          message: this.$t("进行usbkey的口令验证错误!"),
        });
        this.$message.error(error);
        this.loading(false)
      }
      if(this.loginrtn){
        this.$message({
          type: "danger",
          message: this.$t("验证证书内容为空!"),
        });
        this.loading(false)
      }
      //判断是否第一次登录
      if(this.loginrtn.match("#.")){
        if(this.loginrtn=="#2"){
          this.loading(true)//开始登录
          var rtn2=window.usb_ocx.Vgetname(this.pincode,this.caserverip,"1")
          if(rtn2.match("#.")){//是否包含#
            if(rtn2=="#8"){
              this.$message({
                type: "danger",
                message: this.$t("Pin码输入错误!"),
              });
            }else{
              this.$message({
                type: "danger",
                message: this.$t("没有插入key或者key错误!"),
              });
              this.$refs.pincode.focus()
            }
          }else{//进行登录操作
            this.loginToken();
          }
          this.loading(false)//登录完成
        }else{//进行登录操作
          this.loginToken();
        }
      }else{//不是第一次登录则直接登录
        this.loginToken();
      }
    },
    async loginToken(){
      this.getCer();
      this.getSignUser();
      const res = await this.$awaitWraper(this.api.calogin(this.loginForm))
      if(res){
          this.$store.dispatch("organization/user/loginToken",{token:res.token} )
          this.$store.commit('app/SET_ISFIRSTLOAD',true)
          this.$router.push({ path: this.tagWel.value })
        }
        else{
          // 登录失败
          this.$router.push({ path: "/login", replace: true})
        }
    }

  }
}
</script>
